﻿using System.Security.Claims;
using System.Security.Principal;

namespace fabao.Common.Extensions
{
    /// <summary>
    /// Identity相关扩展
    /// </summary>
    public static class IdentityExtension
    {
        public static string FindFirstValue(this ClaimsIdentity identity, string claimType)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }
            var claim = identity.FindFirst(claimType);
            var value = claim?.Value;
            if (value == null)
                throw new ApplicationException("用户登录信息异常");

            return value;
        }

        public static int GetUserId(this IIdentity identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            var claimsIdentity = identity as ClaimsIdentity;
            var value = claimsIdentity?.FindFirstValue("sub");

            if (int.TryParse(value, out var userId))
            {
                return userId;
            }
            return -1;
        }

        public static string GetUserName(this IIdentity identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            var claimsIdentity = identity as ClaimsIdentity;
            return claimsIdentity?.FindFirstValue("userName");
        }

        public static string GetLoginName(this IIdentity identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            var claimsIdentity = identity as ClaimsIdentity;
            return claimsIdentity?.FindFirstValue("name");
        }

        public static int GetRoleId(this IIdentity identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            var claimsIdentity = identity as ClaimsIdentity;
            var value = claimsIdentity?.FindFirstValue(ClaimTypes.Role);

            if (int.TryParse(value, out var roleIdId))
            {
                return roleIdId;
            }
            throw new InvalidCastException("identity角色id转换异常");
        }

        public static int GetGroupId(this IIdentity identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            var claimsIdentity = identity as ClaimsIdentity;
            var value = claimsIdentity?.FindFirstValue("group");
            if (int.TryParse(value, out var groupId))
            {
                return groupId;
            }
            throw new InvalidCastException("identity群组id转换异常");
        }
        public static int GetBlockId(this IIdentity identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            var claimsIdentity = identity as ClaimsIdentity;
            var value = claimsIdentity?.FindFirstValue("block");
            if (int.TryParse(value, out var blockId))
            {
                return blockId;
            }
            throw new InvalidCastException("identity区域id转换异常");
        }
    }
}
